<?php
namespace Home\Controller;
use Common\Controller\HomebaseController;
use Home\Service\ClockTimeService;
use Home\Service\AccountService;
// +----------------------------------------------------------------------
// | Date:2017年2月17日
// +----------------------------------------------------------------------
// | Author: EK_熊<1439527494@qq.com>
// +----------------------------------------------------------------------
// | Description: 此文件作用于****
// +----------------------------------------------------------------------
class ApprovalController extends HomebaseController{
    public function __construct(){
        parent::__construct();
        $this->model = D('Approver');
        if (!$this->approver) $this->redirect('Index/index');
    }
    
    public function index(){
        $this->display();
    }
    
    
    public function approverList(){
        $status = I('status',0);
        $page = I('page',1);
        $limit = I('limit',10);
        $where['approver_sn'] = $this->user_sn;
        $where['aly.status'] = $status;
        if ($status !== '') $where['app_status'] = $status;
        $join = [' __APPLY__ aly ON aly.id = app.apply_id'];
        $field ='aly.*,approver_sn,app_status';
        $list = $this->model->alias('app')->where($where)->join($join)->field($field)->page($page,$limit)->order('created_time desc')->select();
        foreach ($list as $k =>$v){
            $ids[] = $v['id'];
            $vcode = $v['vacate_type'] ? $v['vacate_type'] : $v['type'];
            $list[$k]['vacate_type_name'] = get_vacation_name_ch($vcode);
                  
        }
        
        if ($ids) {
            $map_audit['approver_sn'] = ['neq',$this->user_sn];//当前用户的审核状态不需要查看
            $map_audit['apply_id'] = ['in',$ids];
            $auditBox = $this->model->where($map_audit)->select();
        }
        foreach ($list as $k=>$v){
            $list[$k]['audit'] = [];
            foreach($auditBox as $j){
                if ($j['apply_id'] == $v['id']){
                    
                    $list[$k]['audit'][] = $j;
                }
            }
        }
        $data['list'] = $list;
        $data['ids'] = implode(',',$ids);
        $this->ajaxReturn($data);
    }
    
    /**
     * 批量审核
     * 
     * date:2017年2月21日
     * author: EK_熊
     */
    public function auditAll(){ 
        $ids = I('ids');
        if (empty($ids) || $ids == 'null') $this->error('暂无申请需要处理！！');
        $ids = explode(',',$ids);
        $error = '';
        $err_appid = [];
        foreach ($ids as $appid){
            
            $ret = $this->model->action_pass($appid,$this->user_sn);
            if (!$ret){
                $err_appid[] = $appid;
            }
        }
        if ($err_appid){
            $err_appid_str = implode(',',$err_appid);
            $this->error("批处理未完成，其中ID[$err_appid_str]处理出错！！");
            exit();
        }
        $this->success('操作完成！');            
    }
    
    /**
     * 审核通过，操作单条记录
     * 
     * date:2017年2月27日
     * author: EK_熊
     */
    public function auditPass(){
         
         $appid = I('id');
         $ret = $this->model->action_pass($appid,$this->user_sn);
         if (!$ret) $this->error($this->model->getError());
         $this->success('操作完成！');
        
    }
    
    /**
     * 审核拒绝：操作单条记录 
     * 
     * date:2017年2月28日
     * author: EK_熊
     */
    public function auditReject(){
        $appid = I('id');
        $rejectReason = I('reject_reason');
        $applyModel = M('Apply');
        $save = [
            'updated_time'=>CURRENT_TIME,
            'app_status'=>2
        ];
        if ($rejectReason) $save['reject_reason'] = $rejectReason;
        $ret = $this->model->where(['apply_id'=>$appid,'approver_sn'=>$this->user_sn])->save($save);
        if (!$ret) $this->error('审核数据更新失败！');
        $applyInfo  = $applyModel->where(['id'=>$appid])->find();
        if ($applyInfo['status'] !== 2) {
            $applyModel->where(['id'=>$appid])->setField('status',2);
        }
        
        //系统提交的申请的拒绝操作
        if ($applyInfo['type'] == 5) {
            $re_overtime = $this->_reject_overtime_sys($applyInfo);
            if (!$re_overtime) $this->error('拒绝操作失败！！');
        }
        $this->model->solve_send_msg($appid,2);
        $this->success('操作完成！');        
    }   
    
    
    
    /**
     * 通宵加班，系统自动提交的拒绝操作
     * 
     * 减去申请数据提供的分钟数
     * @param unknown $applyid
     * date:2017年4月24日
     * author: EK_熊
     */
    private  function _reject_overtime_sys($applyInfo){
        //拒绝操作
        $attDanceDay = M('AttendanceDay');
        $date = date('Y-m-d',strtotime($applyInfo['start_time']));
        $map['sn'] = $applyInfo['sn'];
        $map['date'] = $date;
        $duty_off = "$date 23:59:59";
        
        $attendance = $attDanceDay->where($map)->find();
        $clock = new ClockTimeService($attendance['duty_on'],$duty_off);
        $attInfo = $clock->attendance_info();
        $attInfo['duty_off'] = $duty_off;
        $ret = $attDanceDay->where($map)->save($attInfo);
        return $ret;
        
        
    }
    

    
    
    

}